Attribute VB_Name = "Module1"
 'Base64 Simply
 'Convets 111111-11 1111-1111 11-111111 (24bit in 3, 8bit segments)
 'to      111111 11-1111 1111-11 111111 (24bit in 4, 6bit segments)
 '         Num1    Num2    Num3   Num4
 'If the number is 0-25 it assigns capital leters A-Z
 'If the number is 26-51 it assigns small leters a-z
 'If the number is 52-61 it assigns numbers 0-9
 'If the number is 62 it assigns a plus sign
 'If the number is 63 it assigns a division sign
 
Function Base64_Encode3Bytes(Byte1 As Byte, Byte2 As Byte, Byte3 As Byte) As String
 Dim No(1 To 4) As Byte, B(1 To 4) As String

 No(1) = (Byte1 And &HFC&) \ 4
 No(2) = (Byte1 And &H3&) * &H10& + (Byte2 And &HF0&) \ &H10
 No(3) = (Byte2 And &HF&) * &H4& + (Byte3 And &HC0&) \ &H40
 No(4) = (Byte3 And &H3F&)
 For i = 1 To 4
  If No(i) < 26 Then
    B(i) = Chr(65 + No(i))
  ElseIf No(i) < 52 Then
    B(i) = Chr(97 + No(i) - 26)
  ElseIf No(i) < 62 Then
    B(i) = No(i) - 52
  ElseIf No(i) = 62 Then
    B(i) = "+"
  ElseIf No(i) = 63 Then
    B(i) = "/"
  End If
 Next
 Base64_Encode3Bytes = B(1) & B(2) & B(3) & B(4)
End Function

Function EncodeFile(Filename As String) As String
   Dim FileData() As Byte, Out As String, LineCount As Integer
   Open Filename For Binary As #1
      ReDim FileData(LOF(1) - 1)
      Get #1, , FileData
   Close #1
   For i = 0 To UBound(FileData) Step 3
       If i + 2 <= UBound(FileData) Then
         Out = Out & Base64_Encode3Bytes(FileData(i), FileData(i + 1), FileData(i + 2))
       ElseIf i + 1 = UBound(FileData) Then
         Out = Out & Mid(Base64_Encode3Bytes(FileData(i), FileData(i + 1), 0), 1, 3) & "="
       ElseIf i = UBound(FileData) Then
         Out = Out & Mid(Base64_Encode3Bytes(FileData(i), 0, 0), 1, 2) & "=="
       End If
       'Insert a vbcrlf every 76 chars
       If (Len(Out) - LineCount * 2) Mod 76 = 0 Then
          Out = Out & vbCrLf
          LineCount = LineCount + 1
       End If
   Next
   EncodeFile = Out
End Function


Public Sub FileTracking()

    Dim objFSO As New FileSystemObject
    Dim objFile As File
    Dim objSheet As Worksheet
    
    Set objSheet = ActiveSheet
    
    For Each objFile In objFSO.GetFolder("G:\CI").Files
        If Split(objFile.Name, "_")(0) >= "639715360119960" Then
            Debug.Print objFile.Name
            ActiveCell = "'" & Split(objFile.Name, "_")(0)
            objSheet.Cells(ActiveCell.Row + 1, ActiveCell.Column).Activate
            
            ' Exit For
        End If
        DoEvents
    Next objFile
    
End Sub


Public Sub Norwex_Trans()

    Dim objFSO As New FileSystemObject
    Dim objFileIn As File
    Dim objFileOut As TextStream
    Dim strLine As String
    Dim colUnitPrice As New Collection
    Dim strTrans() As String
    Dim i As Long
    Dim strPartnum As String
    Dim strUnitValue As String
    Dim strInstance As String
    
    Set objFileOut = objFSO.OpenTextFile("C:\Users\563774\Documents\Part2.in", ForWriting, True)
    
    With objFSO.OpenTextFile("C:\Users\563774\Documents\Part1.in", ForReading, False)
        Do
            If .AtEndOfStream Then Exit Do
            strLine = .ReadLine
            strTrans = Split(strLine, """")
            
            ' Standard unit price
            For i = 0 To UBound(strTrans)
                If strTrans(i) = "1275," Then
                    strPartnum = strTrans(i + 1)
                    strInstance = 1
                ElseIf 0 <> InStr(strTrans(i), "-") Then
                    If Split(strTrans(i), "-")(0) = 1275 Then
                        strPartnum = strTrans(i + 1)
                        strInstance = Split(strTrans(i), "-")(1)
                    End If
                End If
                If strTrans(i) = "1030," Or Left(strTrans(i), 5) = "1030-" Then
                    If strPartnum = "" Then
                        Debug.Assert False
                    Else
                        ' Make sure same instance
                        If Left(strTrans(i), 5) = "1030-" Then
                            If strInstance <> Split(strTrans(i), "-")(1) Then
                                Debug.Assert False
                            End If
                        End If
                        strUnitValue = ""
                        On Error Resume Next
                        strUnitValue = colUnitPrice.Item(strPartnum)
                        On Error GoTo 0
                        If strUnitValue <> "" Then
                            If strTrans(i + 1) <> strUnitValue Then
                                Debug.Print Now, "Price change, part " & strPartnum & ", was " & strTrans(i + 1) & ", now " & strUnitValue
                                strTrans(i + 1) = strUnitValue
                            End If
                        Else
                            colUnitPrice.Add strTrans(i + 1), strPartnum
                        End If
                        strPartnum = ""
                        strUnitValue = ""
                        strInstance = ""
                    End If
                End If
            Next i
        
            ' Trans out
            strLine = strTrans(0)
            For i = 1 To UBound(strTrans)
                strLine = strLine & """" & strTrans(i)
            Next i
            ' I expect the last trans piece is blank
            
            ' Line out
            objFileOut.WriteLine strLine
            
        Loop
        .Close
        objFileOut.Close
    End With
    
End Sub
